package com.woniuxy.service.impl;

import com.woniuxy.dao.TestDao;
import com.woniuxy.entity.ExamPaper;
import com.woniuxy.entity.Integral;
import com.woniuxy.entity.TestExam;
import com.woniuxy.dao.TestExamDao;
import com.woniuxy.service.TestExamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TestExam)表服务实现类
 *
 * @author zjx
 * @since 2021-05-14 19:41:34
 */
@Service("testExamService")
public class TestExamServiceImpl implements TestExamService {
    @Resource
    private TestExamDao testExamDao;
    @Resource
    private TestDao testDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TestExam queryById(int id) {
        return this.testExamDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<TestExam> queryAllByLimit(int offset, int limit) {
        return this.testExamDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param testExam 实例对象
     * @return 实例对象
     */
    @Override
    public TestExam insert(TestExam testExam) {
        this.testExamDao.insert(testExam);
        return testExam;
    }

    /**
     * 修改数据
     *
     * @param testExam 实例对象
     * @return 实例对象
     */
    @Override
    public TestExam update(TestExam testExam) {
        this.testExamDao.update(testExam);
        return this.queryById(testExam.getTestId());
    }

    /**
     * 通过主键删除数据
     *
     *
     * @return 是否成功
     */
    @Override
    public boolean deleteById() {
        return this.testExamDao.deleteById() > 0;
    }

    @Override
    @Transactional
    public String addRandomTest(String testBankName, String testLabel, ExamPaper examPaper) {
        int singleNumber = examPaper.getSingleTotalScore()/examPaper.getSingleCount();
        String type = "单选题";
        List<Integer> singleIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,singleNumber,type);
        System.out.println("单选题题号集合"+singleIdList);

        int multipleNumber = examPaper.getMultipleTotalScore()/examPaper.getMultipleCount();
        type = "多选题";
        List<Integer> multipleIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,multipleNumber,type);
        System.out.println("多选题题号集合"+multipleIdList);

        int judgeNumber = examPaper.getJudgeTotalScore()/examPaper.getJudgeCount();
        type = "判断题";
        List<Integer> judgeIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,judgeNumber,type);
        System.out.println("判断题题号集合"+judgeIdList);

        int fillBlankNumber = examPaper.getFillBlankTotalScore()/examPaper.getFillBlankCount();
        type = "填空题";
        List<Integer> fillBlankIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,fillBlankNumber,type);
        System.out.println("填空题题号集合"+fillBlankIdList);

        int shortAnswerNumber = examPaper.getShortAnswerTotalScore()/examPaper.getShortAnswerCount();
        type = "问答题";
        List<Integer> shortAnswerIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,shortAnswerNumber,type);
        System.out.println("简答题题号集合"+shortAnswerIdList);

        List<Integer> list = new ArrayList<>();
        list.addAll(singleIdList);
        list.addAll(multipleIdList);
        list.addAll(judgeIdList);
        list.addAll(fillBlankIdList);
        list.addAll(shortAnswerIdList);
        HashMap<String,Object> map = new HashMap<>();
        map.put("ids",list);
        map.put("paperId",examPaper.getId());

        int row  = this.testExamDao.insertTestsByPaper(map);
        String result = "插入试卷试题失败";
        if(row>=1){
            result = "插入试卷试题成功";
        }
        return result;
    }
}
